/*
 *  Copyright 2010 Alexey Frishman (alexey.frishman@gmail.com)
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 *  use this file except in compliance with the License. You may obtain a copy of
 *  the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *  License for the specific language governing permissions and limitations under
 *  the License.
 * 
 */

package org.json4g.serialization.parameterized;

import static org.json4g.TypeUtil.fakeField;

import java.util.Collection;

import org.json4g.serialization.AbstractJsonSerializer;
import org.json4g.serialization.JsonSerializer;
import org.json4g.serialization.policy.SerializationPolicy;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONValue;
import com.gwtent.reflection.client.Field;
import com.gwtent.reflection.client.Type;
import com.gwtent.reflection.client.impl.ClassTypeImpl;

public abstract class JsonCollectionSerializer extends AbstractJsonSerializer {

    private JsonSerializer itemSerializer;
    private SerializationPolicy policy;

    /**
     * 
     * @param itemSerializer
     *            serializer, which will be used handle items in the collection
     * @param policy
     *            serialization rules
     * @param supportedTypes
     *            classes, supported for serialization by this serializer
     */
    public JsonCollectionSerializer(JsonSerializer itemSerializer, SerializationPolicy policy, Type... supportedTypes) {
        super(supportedTypes);
        this.itemSerializer = itemSerializer;
        this.policy = policy;
    }

    
    @Override
    public Object deserialize(JSONValue json, Field field) {
        assertSupported(json, field.getType());
        ClassTypeImpl typeOfElements = (ClassTypeImpl) policy.getGenericTypeExtractor().extract(field);
        return deserialize(json, typeOfElements);
    }
    
    @SuppressWarnings("unchecked")
    public <E> Collection<E> deserialize(JSONValue json, ClassTypeImpl typeOfElements) {
        assertSupported(json);
        JSONArray jsonArray = json.isArray();
        Collection collection = createNewInstance(jsonArray.size());
        for (int i = 0; i <= jsonArray.size() - 1; i++) {
            collection.add(itemSerializer.deserialize(jsonArray.get(i), fakeField(typeOfElements)));
        }
        return collection;
    }

    @Override
    public JSONValue serialize(Object object) {
        assertSupported(object);
        Collection<?> collection = (Collection<?>) object;
        JSONArray json = new JSONArray();
        int i = 0;
        for (Object item : collection) {
            json.set(i, itemSerializer.serialize(item));
            i++;
        }
        return json;
    }

    @Override
    protected boolean supports(JSONValue json) {
        return super.supports(json) && json.isArray() != null;
    }
    
    protected abstract Collection<?> createNewInstance(int size);

}
